package com.mall.common.core.model.response;

import com.mall.common.core.cons.HttpCode;
import lombok.*;

import javax.servlet.http.HttpServletResponse;

/**
 * 成功返回
 * GET: 200 OK
 * POST: 201 Created
 * PUT: 200 OK
 * PATCH: 200 OK
 * DELETE: 204 No Content
 * 接口返回(多态)
 */
@Getter
@ToString
@Builder
@EqualsAndHashCode(callSuper = false)
@AllArgsConstructor
@NoArgsConstructor
public class SuccessResponses<T> extends ApiResponses<T> {

    private static final long serialVersionUID = 1L;
    /**
     * http 状态码
     */
    private Integer status;
    /**
     * 结果集返回
     */
    private T result;

    /**
     * 不需要返回结果
     *
     * @param status
     */
    public static SuccessResponses<Void> success(HttpServletResponse response, int status) {
        response.setStatus(status);
        return SuccessResponses.<Void>builder().status(status).build();

    }

    /**
     * 成功返回
     *
     * @param object
     */
    public static <T> SuccessResponses<T> success(HttpServletResponse response, T object) {
        return success(response, HttpCode.SC_OK, object);

    }

    /**
     * 成功返回
     *
     * @param status
     * @param object
     */
    public static <T> SuccessResponses<T> success(HttpServletResponse response, int status, T object) {
        response.setStatus(status);
        return SuccessResponses.<T>builder().status(status).result(object).build();

    }

    /**
     * 成功返回
     *
     * @param status
     * @param object
     */
    public static <T> SuccessResponses<T> successA(Integer status, T object) {
        return SuccessResponses.<T>builder().status(status).result(object).build();
    }

}
